<template>
  <div class="api">
    <!--表格区域-->
    <el-row style="margin-top: 25px">
      <el-col :span="24">
        <el-table
          @selection-change="handleSelectionChange"
          :data="tableData" style="width: 100%"
          :header-cell-style="{'background-color':'#F2F6FC'}" empty-text="暂无数据">
          <el-table-column prop="account" label="账户"> </el-table-column>
          <el-table-column prop="name" label="呢称"> </el-table-column>
          <el-table-column prop="avatar" label="头像" >
          <template  #default="scope">
            <el-avatar :size="51" :src="scope.row.avatar"/>
          </template>
          </el-table-column>
          <el-table-column prop="phone" label="手机号" width="120"> </el-table-column>
          <el-table-column prop="ip" label="IP" width="120"> </el-table-column>
          <el-table-column prop="address" label="位置"> </el-table-column>
          <el-table-column prop="isp" label="运营商"> </el-table-column>
          <el-table-column prop="system" label="操作系统" >
            <template #default="scope">
              <el-tag>{{scope.row.system}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="mobileTerminal" label="操作设备" >
            <template #default="scope">
              {{scope.row.mobileTerminal ? '移动端' : 'PC'}}
            </template>
          </el-table-column>
          <el-table-column prop="browser" label="浏览器" ></el-table-column>
          <el-table-column prop="ruleNameList" label="角色" align="center">
            <template  #default="scope">
              <span v-for="(item,index)  in scope.row.roleList" :key="index" style="margin-left: 4px">
                <el-tag>{{item.roleAlias}}</el-tag>
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="timeStamp" label="最新访问" align="center">
            <template  #default="scope">
              {{getDateString(scope.row.timeStamp)}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100">
            <template #default="scope">
              <el-popconfirm title="确认下线该用户吗？" @confirm="offline(scope.row.account)" v-if="buttonPermissions('online:offline')">
                <template #reference>
                  <el-button
                    icon="Bottom"
                    style="color: red"
                    type="text">下线</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row style="margin-top: 15px" justify="end">
      <el-col >
        <el-pagination
          background
          style="float: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="params.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import DataAdd from '@/components/user/DataAdd'
import DataDetails from '@/components/user/DataDetails'
import DataUpdate from '@/components/user/DataUpdate'
import { ElMessage } from 'element-plus'
export default {
  name: 'Index',
  components:{
    DataUpdate,
    DataDetails,
    DataAdd
  },
  data () {
    return {
      params: {
        current: 1,
        size: 10,
        roleName: '',
        roleAlias: ''
      },
      form:{
        id:'',
        newPassword:''
      },
      loading:false,
      dialogFormPassword:false,
      drawer:{
        title:'',
        enable:false,
        add:false,
        details:false,
        update:false,
        data:null
      },
      rules:{
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
      },
      total:0,
      tableData: [],
      selectId: []
    }
  },
  computed:{
    //本页面按钮集合,表示该用户在该页面拥有的按钮
    buttonList(){
      let buttonList = this.$store.getters.buttonList
      //过滤获得当前页面配置
      let filter = buttonList.filter(data => data.path === this.$route.path)
      //获得实际按钮
      let buttons = filter.length === 0 ? null : filter[0].children
      if (buttons === null){
        return null
      }
      //获得按钮权限标记
      return buttons.map(data => data.permissionId)
    }
  },
  mounted () {
    this.getDataList()
  },
  methods:{
    //按钮权限判断
    buttonPermissions(buttonTag){
      return  this.buttonList === null ? false :this.buttonList.indexOf(buttonTag) >= 0
    },
    //下线用户
    offline(account){
      this.$http.post('/api/system/offline?account=' + account).then(res =>{
        ElMessage({
          type: 'success',
          showClose: true,
          message: res.data.message,
        })
      })
    },
    //获得页面数据
    getDataList(){
      this.$http.get('/api/system/onLine',{params:this.params}).then(res =>{
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      })
    },
    //表格数据选中
    handleSelectionChange(val){
     this.selectId = val.map(data => data.id)
    },
    // 分页-页面大小
    handleSizeChange(val) {
     this.params.size = val
      this.getDataList()
    },
    // 分页-当前页面
    handleCurrentChange(val) {
      this.params.current = val
      this.getDataList()
    },
    //获得时间部分字符串
    getDateString(timeStamp){
      let date = new Date(timeStamp);
      let hours = date.getHours()
      let minutes = date.getMinutes()
      let seconds = date.getSeconds()
      return hours + ":" + minutes + ":" + (seconds < 10 ? '0' + seconds : seconds);
    }
  }
}
</script>

<style scoped>
.so {
  margin-left: 10px;
  font-size: 14px;
  font-weight: 600;
}
.api {
  width: 100%;
}
</style>
